package com.ukao.steward.dialog;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.baoyz.actionsheet.ActionSheet;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.ukao.steward.R;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.util.Utils;
import com.ukao.steward.widget.StateImageView;

import java.text.ParseException;
import java.util.Calendar;

import static com.ukao.steward.R.id.close_btn;
import static com.ukao.steward.R.id.view;
import static com.ukao.steward.util.MyDateUtils.getTime;

/**
 * Created by ukaodev on 2017/10/14.
 */

public class PickerViewDialog {



    private   PickerViewDialog(){}

    public static     class   SingletonInstance {
        private static final PickerViewDialog INSTANCE = new PickerViewDialog();
    }

    public static synchronized  PickerViewDialog getInstance() {
        return SingletonInstance.INSTANCE;
    }

    public   void showActionSheetDialog(  FragmentManager fragmentManager,
                                             String[] buttonitem, ActionSheet.ActionSheetListener listener ){
        ActionSheet.createBuilder(Utils.getContext(),fragmentManager )
                .setCancelButtonTitle("取消")
                .setOtherButtonTitles(buttonitem)
                .setCancelableOnTouchOutside(true)
                .setListener(listener).show();

    }


    public  void showDatePicker(String birthday, String timeType, Context mContext, TimePickerView.OnTimeSelectListener mOnTimeSelectListener ) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();

        try {


            if (!CheckUtils.isEmpty(birthday)){
                selectedDate.setTime(getTime(birthday, timeType));
            }else{
                selectedDate.add(Calendar.YEAR,-25);
                selectedDate.setTime(selectedDate.getTime());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar startDate = Calendar.getInstance();
        //0代表1月
        startDate.set(1900,0,1);
        Calendar endDate = Calendar.getInstance();
        TimePickerView    pvCustomTime= new TimePickerView.Builder(mContext, mOnTimeSelectListener)

                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .setLabel("年", "月", "日", "", "", "")
                .setType(new boolean[]{true, true, true, false, false, false})
                .build();
        pvCustomTime.show();

    }


    /**
     * 下单时间段
     * @param showDays
     * @param mRightBt3
     * @param mRightBt1
     * @param mContext
     */
    public void showDateQuantumPicker(final int showDays,final TextView mRightBt3,final  TextView mRightBt1,Context mContext,
                                      TimePickerView.OnTimeSelectListener mOnTimeSelectListener) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();
        try {

            String time = showDays > 7 ? mRightBt3.getText().toString() : mRightBt1.getText().toString();
            time = Calendar.getInstance().get(Calendar.YEAR) + "—" + time;
            selectedDate.setTime(getTime(time, "yyyy—MM—dd EEE"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar startDate = Calendar.getInstance();
        startDate.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        Calendar endDate = Calendar.getInstance();
        endDate.add(Calendar.DATE, showDays);
//        endDate.set(Calendar.getInstance().get(Calendar.YEAR), 12,31);
        //时间选择器 ，自定义布局

        TimePickerView      pvCustomTime = new TimePickerView.Builder(mContext, mOnTimeSelectListener
        )
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .setLabel("", "月", "日", "", "", "")
                .setType(new boolean[]{false, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
        pvCustomTime.show();

    }
    /**
     *城市选择器
     */
    public OptionsPickerView ShowCityPickerViewDe(Context _mActivity,String  titile,
                                                OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener,int option1,int option2,int option3) {


        L.i("option1="+option1);
        OptionsPickerView pvOptins = new OptionsPickerView.Builder(_mActivity, mOnOptionsSelectListener)
                .setOutSideCancelable(true)// default is true
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setTitleText(titile)//标题
                .setSelectOptions(option1,option2,option3)
                .setCyclic(true, false, false)//循环与否
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setOutSideCancelable(false)//点击外部dismiss default true
                .build();


        return  pvOptins;
    };



    public OptionsPickerView ShowCityPickerView(Context _mActivity,String  titile,
                                                OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener) {

        OptionsPickerView pvOptins = new OptionsPickerView.Builder(_mActivity, mOnOptionsSelectListener)
                .setOutSideCancelable(true)// default is true
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setTitleText(titile)//标题
                .setCyclic(true, false, false)//循环与否
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setOutSideCancelable(false)//点击外部dismiss default true
                .build();


        return  pvOptins;
    };


    public  void showAnalyzeDatePicker(String birthday,String timeType,boolean isDay, Context  mContext,TimePickerView.OnTimeSelectListener mOnTimeSelectListener ) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();

        try {


            if (!TextUtils.isEmpty(birthday)){
                selectedDate.setTime(getTime(birthday, timeType));
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        String labelDay="";

        boolean[] type;
        if (isDay){
            labelDay="日";
            type = new boolean[]{true, true, true, false, false, false};
        }else{

            type = new boolean[]{true, true, false, false, false, false};
        }

        Calendar startDate = Calendar.getInstance();
        //0代表1月
        startDate.set(2000,1,1);
        Calendar endDate = Calendar.getInstance();

        TimePickerView pvCustomTime = new TimePickerView.Builder(mContext, mOnTimeSelectListener)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .setLabel("年", "月", labelDay, "", "", "")
                .setType(type)
                .build();
        pvCustomTime.show();

    }
}
